use std::{collections::HashMap};
struct Solution {}

impl Solution {
  pub fn contains_nearby_duplicate(nums: Vec<i32>, k: i32) -> bool {
    if nums.len() < 2 || k == 0 {
        return false;
    }
    let l = nums.len();
    let mut k = k as usize;
    if k > l {
        k = l;
    }
    let mut map = HashMap::new();
    for i in 0..k {
      if map.contains_key(&nums[i]) {
        return true
      }
      map.insert(nums[i], 1);
    }
    let mut s: usize = 0;
    while s + k < l {
      if map.contains_key(&nums[s + k]) {
        return true
      }
      map.remove(&nums[s]);
      map.insert(nums[s + k], 1);
      s += 1;
    }
    false
  }
}